package com.zjitc.config;

import com.zjitc.mapper.UserMapper;
import javax.sql.DataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperFactoryBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 * Create by IntelliJ IDEA
 *
 * @author: jsonor
 * @date-Time: 2018/3/8 15:48
 * @description:
 */
@Configuration
@ComponentScan(basePackages = "com.zjitc.config")
public class MyBatisAnnotationCongiguration {
  //以java的方式进行配置
  @Bean
  public SqlSessionFactoryBean sqlSessionFactoryBean(DataSource dataSource){
    SqlSessionFactoryBean sqlSessionFactoryBean=new SqlSessionFactoryBean();
    sqlSessionFactoryBean.setDataSource(dataSource);

    return sqlSessionFactoryBean;
  }

  @Bean
  public MapperFactoryBean<UserMapper> mapperMapperFactoryBean(SqlSessionFactory sqlSessionFactory){
    MapperFactoryBean<UserMapper> mapperFactoryBean=new MapperFactoryBean<UserMapper>();
    mapperFactoryBean.setMapperInterface(UserMapper.class);
    mapperFactoryBean.setSqlSessionFactory(sqlSessionFactory);

    return mapperFactoryBean;
  }
}
